import { defineStore } from 'pinia'
import type { userState } from '@/store/type'
import type { UserModel } from '@/api/user/type'
import staticRouter from '@/router/routes'

const useUserStore = defineStore('user-store', {
  state: (): userState => ({
    token: localStorage.getItem('token') ?? '',
    info: JSON.parse(localStorage.getItem('user') ?? '{}'),
    staticRouter: staticRouter
  }),
  actions: {
    setToken(token: string) {
      this.token = token
      localStorage.setItem('token', token)
    },
    setUserInfo(info: UserModel) {
      this.info = info
      localStorage.setItem('user', JSON.stringify(info))
    },
    logout() {
      this.token = ''
      this.info = {} as UserModel
      localStorage.removeItem('token')
      localStorage.removeItem('user')
    }
  },
  getters: {}
})

export default useUserStore
